"""
    dbCreate.py - Run once to set up the database
    Copyright (C) 2009  Matthew Baldwin <secretagentcook@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>
"""

"""
This file is a stand alone and is only meant to be run once to create the database and set up the tables.
When creating your own mud you WILL need to do some editing of the tables to allow for your
features.
"""
from lxml import etree
from sqlalchemy import *

db_type = 'xml' # Sql and XML are the two support databases.

if db_type == 'sql':
    engine = create_engine('sqlite:///world.sqlite')
    metadata = MetaData(engine)
    players_table = Table('players', metadata,
                        Column('id',  Integer,  primary_key=True),
                        Column('name', String),
                        Column('password', String)
                        )



    room_table = Table('rooms', metadata,
                    Column('number', Integer, primary_key=True),
                    Column('name', String),
                    Column('desc', Text),
                    Column('exits', String),
                    Column('items', String),
                    Column('players', String)
                   )
    metadata.create_all(engine)

    from sqlalchemy.orm import mapper,  sessionmaker

    engine = create_engine('sqlite:///world.sqlite')
    metadata = MetaData(engine)
    Session = sessionmaker(bind=engine)
    session = Session()

    class Players(object):
        def __init__(self, name, password):
            self.name = name
            self.password = password
        
    class Rooms(object):
        def __init__(self, number, name,  desc, exits, items, players):
            self.number = number
            self.name = name
            self.desc = desc
            self.exits = exits
            self.items = items
            self.players = players
    
    players_table = Table('players', metadata, autoload=True)
    mapper(Players, players_table)

    room_table = Table('rooms', metadata, autoload=True)
    mapper(Rooms, room_table)


    admin_player = Players('admin',  1234)
    start_room = Rooms('1', 'Start',  'This room is a sample room',  None, None, None)
    session.add_all([admin_player,  start_room])
    session.commit()

elif db_type == 'xml':
    """
    Here is the file structure for the xml database:

    * players - this is the top container.
      * <player name> - this is the player's name and will hold all the information relating
        to the player.  'admin' is created by default
        * password - this is the player's password.

    * world - this is the base container for rooms.
      * zone list will just keep the list of zones and other data relating to the zones.
      * rooms - holds all the rooms
        * room number
          * room name
          * room description
    """
    player_file = open("players.xml", "w")
    players = etree.Element("players") #creates the parent
    admin = etree.SubElement(players, "admin") #creates the admin player
    admin.text = "1234" #password

    player_file.write(etree.tostring(players, pretty_print=True))
    player_file.close()

    world_file = open("world.xml", "w")
    world = etree.Element("world")

    zone_list = etree.SubElement(world, "ZoneList")
    zone_list.text = "1"

    rooms = etree.SubElement(world, "rooms")

    room_101 = etree.SubElement(rooms, "one_zero_one")
    room_101_name = etree.SubElement(room_101, "name")
    room_101_name.text = "Start"
    room_101_desc = etree.SubElement(room_101, "description")
    room_101_desc.text = "This room is a sample room"

    world_file.write(etree.tostring(world, pretty_print=True))
    world_file.close()